/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Utilidades;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created on : May 21, 2011, 1:49:30 PM
 * @author diogo
 */
@WebServlet(name="UtilityServlet", urlPatterns={"/UtilityServlet"})
public class UtilityServlet extends HttpServlet {
   // <editor-fold defaultstate="collapsed" desc="netbeans default template">
////    /**
////     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
////     * @param request servlet request
////     * @param response servlet response
////     * @throws ServletException if a servlet-specific error occurs
////     * @throws IOException if an I/O error occurs
////     */
////    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
////    throws ServletException, IOException {
////        response.setContentType("text/html;charset=UTF-8");
////        PrintWriter out = response.getWriter();
////        try {
////            /* TODO output your page here
////            out.println("<html>");
////            out.println("<head>");
////            out.println("<title>Servlet UtilityServlet</title>");
////            out.println("</head>");
////            out.println("<body>");
////            out.println("<h1>Servlet UtilityServlet at " + request.getContextPath () + "</h1>");
////            out.println("</body>");
////            out.println("</html>");
////            */
////        } finally {
////            out.close();
////        }
////    }
	// </editor-fold>

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StringBuffer output = new StringBuffer("");

		output.append(GetDump(request));
        response.getWriter().print(output);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StringBuffer output = new StringBuffer("");

		output.append(GetDump(request));

        response.getWriter().print(output);
    }


	/**
	 * lists all parameters and it's value
	 * @param request
	 * @return
	 */
	public String GetDump(HttpServletRequest request){
		StringBuilder output = new StringBuilder("");

		Enumeration<String> keys = request.getParameterNames();
		output.append("<html><body>Start Dumping:");
		output.append("<table border = \"1\">");
		output.append(String.format("<tr><td>%s </td><td> %s </td><td> %s </td> </tr>", "#", "Parameter Name", "Value"));
		int x = 0;
		while (keys.hasMoreElements()) {
			String key = keys.nextElement();
			String value = request.getParameter(key);
			String values[] = request.getParameterValues(key);
			
			String shit = "";
			for (String s : values){
				shit = shit + s;
			}
			output.append(String.format("<tr><td>%d </td><td> %s </td><td> %s </td>  <td> %s </td></tr>", x, key, value, shit));
			x++;
		}

		output.append("</table>");
		output.append("End Dumping: ");
		return output.toString();
	}
}
